package cn.bocweb.spinning.view.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.bocweb.spinning.R;
import cn.bocweb.spinning.model.Bean.HelpListBean;
import cn.bocweb.spinning.model.Bean.LibListBean;
import cn.bocweb.spinning.model.Bean.NewFeedbackBean;
import cn.bocweb.spinning.model.Bean.UserBean;
import cn.bocweb.spinning.presenter.Imp.UserPresenterImp;
import cn.bocweb.spinning.presenter.UserPresenter;
import cn.bocweb.spinning.util.Constant;
import cn.bocweb.spinning.util.DeviceUtil;
import cn.bocweb.spinning.view.adapter.HelpAdapter;
import cn.bocweb.spinning.view.common.BaseActivity;
import cn.bocweb.spinning.view.common.SwipeRefreshHelper;
import de.greenrobot.event.EventBus;

public class HelpActivity extends BaseActivity implements View.OnClickListener {

    @Bind(R.id.toolbar)
    Toolbar toolbar;
    @Bind(R.id.listView)
    ListView listView;

    List<HelpListBean.DataEntity> list;
    HelpAdapter adapter;
    @Bind(R.id.help_refresh)
    SwipeRefreshLayout helpRefresh;
    SwipeRefreshHelper swipeRefreshHelper;
    UserPresenter presenter;
    String userid, libid;
    int page = 1;
    boolean isLastPage = false;
    @Bind(R.id.help_write_suggest)
    LinearLayout helpWriteSuggest;
    @Bind(R.id.help_dot)
    RelativeLayout helpDot;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_help);

        EventBus.getDefault().register(this);
        ButterKnife.bind(this);
        loadDate();
        initToolbar();
        initClick();
        initModel();

    }

    private void initModel() {
        UserBean.DataEntity user = new UserBean.DataEntity();
        user.readInfoFromLocal(this);
        userid = user.getUserid();

        LibListBean.DataEntity lib = new LibListBean.DataEntity();
        lib.readLibListIntoLocal(this);
        libid = lib.getId();

        presenter = new UserPresenterImp();
        presenter.helpList("9", String.valueOf(page));
        presenter.newFeedback(userid);
        showLoading();
    }

    private void initClick() {
        helpWriteSuggest.setOnClickListener(this);
    }

    @Override
    protected void onStart() {
        super.onStart();

        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }

        swipeRefreshHelper = new SwipeRefreshHelper(helpRefresh, listView, new SwipeRefreshHelper.OnSwipeRefreshListener() {
            @Override
            public void onRefresh() {
                page = 1;
                isLastPage = false;
                list.clear();
                adapter.notifyDataSetChanged();
                presenter.helpList("9", String.valueOf(page));
                helpRefresh.setRefreshing(false);
            }

            @Override
            public void onLoad() {
                if (!isLastPage) {
                    page++;
                    presenter.helpList("9", String.valueOf(page));
                } else {
                    DeviceUtil.MyToast(HelpActivity.this, "已经是最后一页");
                }
            }
        });
    }

    @Override
    protected void onStop() {
        super.onStop();
        EventBus.getDefault().unregister(this);
    }

    public void onEventMainThread(HelpListBean bean) {
        if (bean.getReturn_code() == 0) {
            list.addAll(bean.getData());
            adapter.notifyDataSetChanged();
        } else if (bean.getReturn_code() == -203) {
            DeviceUtil.MyToast(this, bean.getMsg());
            isLastPage = true;
        } else {
            DeviceUtil.MyToast(this, bean.getMsg());
        }
    }

    public void onEventMainThread(NewFeedbackBean bean) {
        hideLoading();
        if (bean.getReturn_code() == 0) {
            if (bean.getStatus().equals("1")) {
                helpDot.setVisibility(View.VISIBLE);
            } else {
                helpDot.setVisibility(View.GONE);
            }
        }
    }

    private void initToolbar() {
        toolbar.setTitle("帮助及反馈");
        toolbar.setTitleTextColor(getResources().getColor(R.color.white));
        toolbar.setNavigationIcon(R.mipmap.btn_exit);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void loadDate() {
        list = new ArrayList<>();
        adapter = new HelpAdapter(this, list);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(HelpActivity.this, HelpDetailActivity.class);
                intent.putExtra(Constant.HELP_DETAIL, list.get(position).getId());
                startActivity(intent);
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.help_write_suggest: {
                Intent intent = new Intent(this, FeedBackActivity.class);
                startActivity(intent);
                break;
            }
        }
    }
}
